/**
 * @class NG.views.WMSList
 * @extends NG.util.List
 * 
 * This panel is used for listing WM servers.<br>
 * This component is registered as 'netgis-WMSList'.
 */
NG.views.WMSList = Ext.extend(NG.util.List, {
    
    map: null,
    
    createStore: function(){
        Ext.regModel('Layer', {
            fields: ['id', 'name', 'visibility', 'zindex']
        });
        var data = [];
        Ext.each(this.map.layers, function(layer){
            if (layer.displayInLayerSwitcher === true) {
                var visibility = layer.isBaseLayer ? (this.map.baseLayer == layer) : layer.getVisibility();
                data.push({
                    id: layer.id,
                    name: layer.name,
                    visibility: visibility,
                    zindex: layer.getZIndex()
                });
            }
        });
        return new Ext.data.Store({
            model: 'Layer',
            sorters: 'zindex',
            data: data
        });
    },
    
    initComponent: function(){
        this.store = this.createStore();
        this.itemTpl = new Ext.XTemplate(
            '<tpl if="visibility == true">', 
                '<img width="20" src="img/check-round-green.png">', 
            '</tpl>', 
            '<tpl if="visibility == false">', 
                '<img width="20" src="img/check-round-grey.png">', 
            '</tpl>', 
            '<span class="gx-layer-item">{name}</span>'
        );
        this.listeners = {
            itemtap: function(dataview, index, item, e){
                var record = dataview.getStore().getAt(index);
                var layer = this.map.getLayersBy("id", record.get("id"))[0];
                if (layer.isBaseLayer) {
                    this.map.setBaseLayer(layer);
                }
                else {
                    layer.setVisibility(!layer.getVisibility());
                }
                record.set("visibility", layer.getVisibility());
            }
        };
        this.map.events.on({
            "changelayer": this.onChangeLayer,
            scope: this
        });
        NG.views.WMSList.superclass.initComponent.call(this);
    },

    findLayerRecord: function(layer){
        var found;
        this.store.each(function(record){
            if (record.get("id") === layer.id) {
                found = record;
            }
        }, this);
        return found;
    },
    
    onChangeLayer: function(evt){
        if (evt.property == "visibility") {
            var record = this.findLayerRecord(evt.layer);
            record.set("visibility", evt.layer.getVisibility());
        }
    }
    
});
Ext.reg('netgis-WMSList', NG.views.WMSList);

/**
 * @extend {NG.util.List}
 * @class {NG.views.FadOperations}
 * 
 * Used in {@link NG.views.SearchView}
 */
NG.views.FadOperations = Ext.extend(NG.util.List, {
    
    createStore: function(){
        Ext.regModel('FadOpe', {
            fields: ['id', 'name', 'iconFile'],
            isDelete: function (){
            	return this.data.id === 'remove'; 
            },
	        isUpdate: function (){
	        	return this.data.id === 'update'; 
	        },
	        isViewDetail: function (){
	        	return this.data.id === 'viewdetail'; 
	        }

        });
        var data = [{id:'remove', name: L_REMOVE, iconFile: 'delete.png'},
                    {id:'update', name: L_UPDATE, iconFile: 'edit.png'},
                    {id:'viewdetail', name: L_VIEWDETAIL, iconFile: 'information.png'}];
        return new Ext.data.Store({
            model: 'FadOpe',
            data: data
        });
    },
    
    initComponent: function(){
        this.store = this.createStore();
        this.itemTpl = new Ext.XTemplate(
            '<tpl>', 
                '<img width="20" src="img/{iconFile}">', 
            '</tpl>', 
            '<span class="gx-layer-item">{name}</span>'
        );
        
        NG.views.FadOperations.superclass.initComponent.call(this);
    }
    
});

/**
 * Used in search view.
 */
Ext.reg('netgis-FadOperationList', NG.views.FadOperations);

